<?php
namespace jianan\controllers;

use Yii;
use yii\web\Controller;
use yii\data\Pagination;
use jianan\models\Room;
use jianan\models\Floor;
use jianan\models\Master;
use jianan\models\Cus;

header("Content-type:text/html;charset=utf-8");

class RoomController extends Controller {



	public function actionIndex(){
		$model = new Room;
		
		$floor = Floor::find()->asArray()->all();
		if(isset($_GET['floorId'])){
			$floorid = $_GET['floorId'];	

			$query = Room::find()->where(['floorId'=>$floorid])->asArray()->all();
			//得到单元		
			$unit = [];
			foreach($query as $k=>$v){
				$unit['unitNo'][$v['unitNo']] = $v['unitName'];
				$unit['tier'][$this->getTier($v['roomId'])] = array();
			}

			foreach( $unit as $k => $v) {
				foreach($query as $key => $val) {
						$unit['rooms'][$val['unitNo']][$this->getTier($val['roomId'])][$val['id']] = $val;
				}
			}

			// var_dump($unit);
			// exit;
			$unitNo = $unit['unitNo'];
			$tier = $unit['tier'];
			$rooms = $unit['rooms'];
			return $this->render('index',[
				'model' => $model,
				'floor' => $floor,
				'unitNo' => $unitNo,
				'tier' => $tier,
				'rooms' => $rooms,
				'floorId' => $floorid
			]);
		} else {
			return $this->render('index',['floor'=>$floor,'model'=>$model]);
		}
	}

	public function actionSearch(){
		$model = new Room;

		if(!empty($_POST['Room'])){
			
			$re = $this->term($_POST['Room']);
			$query = Room::find();		
			$countQuery = clone $query;
			$count = $countQuery->count();
			$pages = new Pagination(['totalCount'=>$countQuery->count(),'pageSize'=>20]);
			$model = $query->where($re)->offset($pages->offset)->limit($pages->limit)
					->orderBy(['floorNo'=> SORT_ASC])
					->orderBy(['unitNo'=> SORT_ASC])
					->orderBy(['tier'=> SORT_ASC])->all();
			foreach($model as $v){
				$fname = Floor::findOne($v['floorId']);
				$v['floorId'] = $fname['fName'];
			}
			return $this->render('search',['model'=>$model,'pages'=>$pages,'count'=>$count]);

		} else {
			$pages = '';
			return $this->render('search',['pages'=>$pages]);
		}
		
	}

	public function actionDetail(){
		$id = $_GET['id'];
		$floorid = $_GET['floorId'];

		$floor = Floor::findOne($floorid);
		//楼号
		$floorname = $floor['floorNo'];
		//详细信息
		$model = Room::find()->where(['id'=>$id])->asArray()->one();
		//跟办顾问
		$master = Master::findOne($model['sid']);
		$mastername = $master->masterName; 
		//业主
		$own = Cus::findOne($model['cusid']);
		if($own){		
			$model['owner'] = $own->name;
			$model['sid'] = $own->IDNo;
		}
		// $owner = $own->name;
		// $sid = $own->IDNo;
		return $this->render('detail',['model'=>$model,'floor'=>$floorname,'master'=>$mastername]);

	
	}

	public function actionDai(){
		if(Yii::$app->request->isAjax){
			$total = $_POST['total'];
			$dai = $_POST['dai']/10;
			$year = (integer)$_POST['year'];
			$lv = $_POST['lv'];


			//贷款总额
			$daitotal = $total*$dai;
			//首期房款
			$shoutotal = $total-$daitotal;
			//每月还款 pow
			$x = 1+$lv/12;
			$y = $year*12;
			$z = pow($x,$y);

			$mdai = ($daitotal*($lv/12)*pow($x,$y))/($z-1);
			//利息总计
			$litotal = $mdai*$y-$daitotal;

			$result = [
				'daitotal' => $daitotal,
				'shoutotal' => $shoutotal,
				'mdai' => $mdai,
				'litotal' =>$litotal
			];
			$re = json_encode($result);
			return $re;
		}
	}

	public function term($term){
		$tier = $term['tier'];
		$Aarea = $term['Aarea'];
		$Earea = $term['Earea'];
		$Atotalprice = $term['Atotalprice'];
		$Etotalprice = $term['Etotalprice'];
		if(isset($tier) && $tier>0 && isset($Aarea) && $Aarea>0 && isset($Earea) && $Earea>0 && isset($Atotalprice) && $Atotalprice>0 && isset($Etotalprice) && $Etotalprice>0){
			$re = [
				'AND',
				['status'=>0],
				['tier'=>$tier],
				['>','area',$Aarea],
				['<','area',$Earea],
				['>','totalprice',$Atotalprice],
				['<','totalprice',$Etotalprice]
			];
		} 
		
		if (isset($tier) && $tier>0 && isset($Aarea) && $Aarea>0 && isset($Earea) && $Earea>0) {
			$re = [
				'AND',
				['status'=>0],
				['tier'=>$tier],
				['>','area',$Aarea],
				['<','area',$Earea]
			];
		}
		if(isset($tier) && $tier>0) {
			$re = [
				'AND',
				['status'=>0],
				['tier' => $tier]
			];
		}
		return $re;
	}

	public function getTier($str) {
		return substr($str, 0, strlen($str)-2);
	}

}
